﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserControlLibrary;
using System.IO;

namespace ReviewUI
{
    public partial class frmReviewUI : Form
    {
        public frmReviewUI()
        {
            InitializeComponent();
            treeListView1.WithCheckbox = true;
            treeListView1.RefreshTLV();            
            treeListView1.EventListCellClick += new EventHandler<UserControlLibrary.EventListCellClickEventArgs>(treeListView1_EventListCellClick);            
            this.FormClosing += new FormClosingEventHandler(frmReviewUI_FormClosing);
            LoadSettings();            
        }
        public void frmReviewUI_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            videoControl1.Stop();
            SaveSettings();
        }
        private void LoadSettings()
        {
            try
            {
                videoControl1.Savefilepath = Properties.Settings.Default.Saveimagefilepath;
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }
        private void SaveSettings()
        {
            try
            {
                Properties.Settings.Default.Saveimagefilepath = videoControl1.Savefilepath;
                Properties.Settings.Default.Save();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }


        private void treeListView1_EventListCellClick(object sender, EventListCellClickEventArgs e)
        {   
            videoControl1.Stop();
            string[] _filenames = new string[3];
            _filenames[0] = e.SelectedRecord.Video1;
            _filenames[1] = e.SelectedRecord.Video2;
            _filenames[2] = e.SelectedRecord.Video3;
           
            videoControl1.FileNames = _filenames;
            videoControl1.Getpath();
            videoControl1.Play();

            DateTime time1          = new DateTime(Math.Max(File.GetCreationTime(_filenames[0]).Ticks, File.GetCreationTime(_filenames[1]).Ticks));
            DateTime Filecreatedate = new DateTime(Math.Max(File.GetCreationTime(_filenames[2]).Ticks, time1.Ticks));
            //DateTime Filecreatedate = File.GetCreationTime(_filenames[1]);
           
            DateTime timetoplay =e.SelectedEvent.EventTime;//Convert.ToDateTime("6/20/2012 11:58:46 AM");
            TimeSpan span = timetoplay - Filecreatedate;
            double videopostion = span.TotalSeconds;
            videoControl1.Setpositon(videopostion);
            
            //textBox1.Text = Filecreatedate.ToString();
            //textBox2.Text = timetoplay.ToString();
            //textBox3.Text = Convert.ToString(videopostion);

        }
        private void Get_creation_time(string filepath)
        {
            DateTime Filecreatedate = File.GetCreationTime(filepath); 
        }

        private void ExpToExcel_Click(object sender, EventArgs e)
        {
            treeListView1.ExportToExcel();
        }
    }
}
